
package com.example.demo.modules.sys.controller;

import com.example.demo.core.entity.ProcessResult;
import com.example.demo.core.entity.ProcessStatHolder;
import com.example.demo.core.result.PageResult;
import com.example.demo.modules.sys.model.SysUser;
import com.example.demo.modules.sys.service.UserService;
import com.github.pagehelper.PageInfo;
import org.apache.catalina.util.RequestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.example.demo.core.entity.ProcessResult.ERROR;

/**
 * @author qjp
 * @since 2015-12-19 11:10
 */
@RestController
@RequestMapping("/sys/user")
public class UserController {

    @Autowired
    private UserService userService;


    @RequestMapping("/list")
    public ModelAndView list() {
       // List<SysUser> userInfoList = userService.getAll(sysUser);
        return new ModelAndView("/modules/sys/sysUser/list");
    }

    @RequestMapping("/form")
    public ModelAndView form() {
        return new ModelAndView("/modules/sys/sysUser/form");
    }

    @RequestMapping
    public PageResult<SysUser> getAll(SysUser sysUser, String keyword) {
        List<SysUser> userInfoList = userService.getAll(sysUser,keyword);
        return new PageResult(new PageInfo<SysUser>(userInfoList));
    }

    @PostMapping(value = "/batchDelete")
    public ProcessResult batchDelete(@RequestParam("ids[]") Integer[] ids) {
        try {
            userService.batchDelete(ids);
            return new ProcessResult();
        }catch (Exception e){
            return new ProcessResult(ERROR,e.getMessage().toString());
        }
    }

    @RequestMapping(value = "/saveOrUpdate")
    public ProcessResult saveOrUpdate(SysUser sysUser) {
        try {
            userService.saveOrUpdate(sysUser);
            return new ProcessResult();
        }catch (Exception e){
            return new ProcessResult(ERROR,e.getMessage().toString());
        }
    }

    @RequestMapping(value = "/view/{id}")
    public SysUser view(@PathVariable Integer id) {
        SysUser userInfo = userService.getById(id);
        return userInfo;
    }

    @RequestMapping(value = "/delete/{id}")
    public ProcessResult delete(@PathVariable Integer id) {
        try {
            userService.deleteById(id);
            return new ProcessResult();
        }catch (Exception e){
            return new ProcessResult(ERROR,e.getMessage().toString());
        }
    }

    @RequestMapping(value = "/allotUser/getIsOwner/{roleId}/{keyword}")
    public Map<String,Object> getIsOwner(@PathVariable Integer roleId,@PathVariable String keyword){
        Map<String,Object> map=new HashMap<>();
        try{
            if("null".equals(keyword)){
                keyword="";
            }
            List<SysUser> list=userService.getIsOwner(roleId,keyword);
            map.put("total",list.size());
            map.put("rows",list);
        }catch (Exception e){
            e.printStackTrace();
        }
        return map;
    }

    @RequestMapping(value = "/allotUser/getIsNotOwner/{roleId}/{keyword}")
    public Map<String,Object> getIsNotOwner(@PathVariable Integer roleId,@PathVariable String keyword){
        Map<String,Object> map=new HashMap<>();
        try{
            if("null".equals(keyword)){
                keyword="";
            }
            List<SysUser> list=userService.getIsNotOwner(roleId,keyword);
            map.put("total",list.size());
            map.put("rows",list);
        }catch (Exception e){
            e.printStackTrace();
        }
        return map;
    }


    @RequestMapping(value = "/allotUserById")
    public ProcessResult allotUserById(Integer userId,Integer roleId,Integer type) {
        try {
            userService.allotUserById(userId,roleId,type);
            return new ProcessResult();
        }catch (Exception e){
            return new ProcessResult(ERROR,e.getMessage().toString());
        }
    }

}
